﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
namespace UIT_CV_Management
{
    public class DaoTaoDaiHoc
    {
        public String id;
        public String maDaoTaoDaiHoc;
        public String maChiTietNoiHoc;

        //database
        Database myDatabase = Database.shareDatabase;

        public ChiTietNoiHoc chiTietNoiHoc;

        public ChiTietNoiHoc getChiTietNoiHoc()
        {
            return new ChiTietNoiHoc(maChiTietNoiHoc);
        }
        public DaoTaoDaiHoc(ChiTietNoiHoc _chiTietNoiHoc)
        {
            // get id form database
            id = Database.getID("DaoTaoDaiHoc", "ID");

            //properties
            maDaoTaoDaiHoc = Database.getID("DaoTaoDaiHoc", "MaDaoTaoDaiHoc");
            maChiTietNoiHoc = _chiTietNoiHoc.maChiTietNoiHoc;

            //object 
            chiTietNoiHoc = _chiTietNoiHoc;
        }
        public DaoTaoDaiHoc(String _id)
        {
            QueryResult result = myDatabase.Query("SELECT * FROM DaoTaoDaiHoc WHERE ID = '" + _id + "'");
            if (result.ReturnCode == 200)
            {
                DataTable table = result.Table;

                id = table.Rows[0]["ID"].ToString();
                maDaoTaoDaiHoc = table.Rows[0]["MaDaoTaoDaiHoc"].ToString();
                maChiTietNoiHoc = table.Rows[0]["MaNoiHoc"].ToString();
            }
        }
        public Boolean Save()
        {

            chiTietNoiHoc.Save();

            String sqlDaoTaoDaiHoc = "INSERT INTO DaoTaoDaiHoc VALUES('" 
                + id + "','" 
                + maDaoTaoDaiHoc + "','" 
                + maChiTietNoiHoc + "')";
            QueryResult result = myDatabase.Query(sqlDaoTaoDaiHoc);
            if (result.ReturnCode == 409)
                return false;
            else // save successfully
            {
                
                return true;
            }
        }
        public Boolean Delete()
        {
            String sqlDelete = "DELETE FROM DaoTaoDaiHoc WHERE ID = '" + id + "'";
            QueryResult result = myDatabase.Query(sqlDelete);

            if (result.ReturnCode == 409)
                return false;
            else
            {
                chiTietNoiHoc.Delete();
                return true;
            }
        }
    }
}
